package main

import (
	"Doge/config"
	"Doge/modules/delayed/master"
	"Doge/routers"
	"flag"
	"fmt"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"net/http"
	"os"
	"runtime"
)

var (
	confFile string
	confMasterFile string
	dirCurrent string
)

func initArgs()  {
	dirCurrent, _ = os.Getwd()
	flag.StringVar(&confFile, "config", "./config/config.json", "指定config.json")
	confMasterFile = dirCurrent + "/modules/delayed/master/master.json"
	flag.Parse()
}

//初始化线程
func initEnv() {
	runtime.GOMAXPROCS(runtime.NumCPU())
}

//分布式延时任务
func main() {
	var (
		e	*echo.Echo
		delayedRoute *echo.Group
		err error
	)

	e = echo.New()


	//log
	e.Use(middleware.Logger())

	//初始化命令行
	initArgs()

	//初始化线程
	initEnv()

	//加载配置
	if err = config.InitConfig(confFile); err != nil {
		fmt.Println("加载配置文件错误:",err.Error())
		panic(err)
	}

	// 加载master配置
	if err = master.InitConfig(confMasterFile); err != nil {
		panic(err)
	}
	//  任务管理器
	if err = master.InitJobMgr(); err != nil {
		panic(err)
	}




	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "Hello, Doge!")
	})

	delayedRoute = e.Group("delayed")
	if err = routers.DelayedRoute(delayedRoute); err != nil {
		panic(err)
	}


	e.Logger.Fatal(e.Start(":8080"))
}
